package com.mk.api.file;

import com.mk.base.model.response.Rsp;
import com.mk.model.request.DirRequest;
import com.mk.service.IDirService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 9:28 AM 3/17/18
 */
@RestController
@RequestMapping("/dir")
public class DirApi {

    @Autowired
    private IDirService dirService;

    @RequestMapping(method = RequestMethod.POST)
    public Rsp saveDir(@RequestBody DirRequest request){
        try{
            return Rsp.SUCCESS(dirService.saveDir(request));
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Rsp updateDir(@RequestBody DirRequest request){
        try{
            if(dirService.updateDir(request))
                return Rsp.SUCCESS("编辑文件夹信息成功");
            else
                return Rsp.ERROR("变价文件夹信息失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Rsp deleteDir(@PathVariable("id") Long id){
        try{
            if(dirService.deleteDir(id))
                return Rsp.SUCCESS("文件夹信息删除成功");
            else
                return Rsp.ERROR("文件夹信息删除失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public Rsp findDirs(@RequestParam(value = "id",required = false) Long id,
                        @RequestParam(value = "userId",required = false)Long userId){
        try{
            if(id != null)
                return Rsp.SUCCESS(dirService.get(id,userId));
            else
                return Rsp.SUCCESS(dirService.findAll(userId));
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(value = "/key/search",method = RequestMethod.GET)
    public Rsp keySearch(@RequestParam("key") String key,@RequestParam("userId")Long userId){
        try{
            return Rsp.SUCCESS(dirService.keySearch(key,userId));
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }
}
